<?php
session_start();
require_once "../includes/common.php";
require_once BASE_PATH.'/class/Action.class.php';
require_once BASE_PATH.'/class/User.class.php';
require_once "../islogin.php";

class ActionApi {
    private $action;


    public function __construct() {
        $this->action = new Action();
    }

    public function handleRequest() {
        if (!isset($_GET['type'])) {
            $this->respond(400, ['error' => '缺少 type 参数']);
            return;
        }

        //安全验证uid
        global $user_info; // 使用 global 关键字引用外部变量
        if($_POST['meuid']){
            //$meuid = $_POST['meuid'];
            if($_POST['meuid']&&!$_POST['fans_id']){
                if ($_POST['meuid']!=$user_info['message']['user_id']) {
                    $this->respond(json_encode(['message' => 'uid error!']));
                    return;
                }
            }

        }else{
            if($_POST['uid']&&!$_POST['fans_id']){
                if ($_POST['uid']!=$user_info['message']['user_id']) {
                    $this->respond(json_encode(['message' => 'uid error!']));
                    return;
                }
            }
        }

        if($_POST['uid']&&$_POST['fans_id']){
            if($_POST['fans_id']!=$user_info['message']['user_id']){
                $this->respond(json_encode(['message' => 'uid error!']));
                return;
            }
        }

        switch ($_GET['type']) {
            case 'like':
                $this->like();
                break;
            case 'fans':
                $this->fans();
                break;
            case 'sohistory':
                $this->sohistory();
                break;
            case 'hideso':
                $this->hideso();
                break;
            case 'sokeywords':
                $this->sokeywords();
                break;
            case 'hotkeywords':
                $this->hotkeywords();
                break;
            case 'hideview':
                $this->hideview();
                break;

            default:
                $this->respond(400, ['error' => '无效的 type 参数']);
                break;
        }
    }

    private function like() {
        $uid = $_POST['uid'] ?? '';
        $nid = $_POST['nid'] ?? '';
        $type = $_POST['type'] ?? '';

        $res = $this->action->userLikeAction($uid,$nid,'',$type);

        $this->respond($res);
    }

    private function fans() {
        $uid = $_POST['uid'] ?? '';
        $fans_id = $_POST['fans_id'] ?? '';

        $res = $this->action->userFans($fans_id,$uid);

        $this->respond($res);
    }

    private function sohistory(){
        $uid = $_POST['uid'] ?? ''; // 用户ID
        $meuid = $_POST['meuid'] ?? '';
        if($meuid){
            $uid = $meuid;
        }
        $page = 1;
        $perPage = 12; // 每页条目数

        $res = $this->action->getUserSearchHistory($uid, $page, $perPage);

        $this->respond($res);
    }

    private function sokeywords(){
        $keywords = $_POST['keywords'] ?? '';
        $page = 1;
        $perPage = 10; // 每页条目数

        $res = $this->action->getSearchKeywords($keywords, $page, $perPage);

        $this->respond($res);
    }

    private function hotkeywords(){
        $page = 1;
        $perPage = 8; // 每页条目数

        $res = $this->action->getTodayHotSearch($page, $perPage);

        $this->respond($res);
    }

    private function hideso(){
        $uid = $_POST['uid'] ?? '';
        $id = $_POST['id'];

        $res = $this->action->hideSearchView($uid,$id);

        $this->respond($res);
    }

    private function hideview(){
        $uid = $_POST['uid'] ?? '';
        $nid = $_POST['nid'];

        $res = $this->action->hideViewAction($uid,$nid);

        $this->respond($res);
    }

    private function respond($data) {
        header('Content-Type: application/json');
        echo $data;
        exit;
    }


}

$api = new ActionApi();
$api->handleRequest();


